//
//  MainTabBarController.swift
//  SinaWeibo--swift
//
//  Created by 蒋春萌 on 15/9/29.
//  Copyright © 2015年 蒋春萌. All rights reserved.
//

import UIKit

class MainTabBarController: UITabBarController {

    @IBOutlet weak var mainTabBar: MainTabBar!
    override func viewDidLoad() {
        super.viewDidLoad()

        mainTabBar.composeButton.addTarget(self, action: "didClickComposeButton", forControlEvents: UIControlEvents.TouchUpInside)
//        添加底部导航控制器
        addChildViewControllers()
    }

//    加号按钮的点击事件
    func didClickComposeButton(){
    
        print(__FUNCTION__)
    }
    
//   接到内存警告后调用
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }

//    添加底部的五个导航控制器
    func addChildViewControllers() {
//        改到AppDelegateL里面写了
//        self.tabBar.tintColor = UIColor.orangeColor()
        addChildViewController("Home", title: "首页", imageName: "tabbar_home")
        addChildViewController("Message", title: "消息", imageName: "tabbar_message_center")
        addChildViewController("Discover", title: "发现", imageName: "tabbar_discover")
        addChildViewController("Profile", title: "我", imageName: "tabbar_profile")
        
    }
    
//    创建一个添加底部导航栏控制器的函数
    func addChildViewController(name: String,title: String, imageName: String) {
        
//        根据storyboard的名称加载控制器
        let sb = UIStoryboard(name: name, bundle: nil)
        
//        获取对应storyboard的初始化控制器
//        as!将初始控制器强制转化为导航控制器
        let nav = sb.instantiateInitialViewController() as! UINavigationController
        
//        设置导航控制器和tabbar控制器的标题名
        nav.topViewController?.title = title
        
//        设置底部导航栏的item图片
        nav.tabBarItem.image = UIImage(named:imageName)
        
        addChildViewController(nav)
    }
}
